package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.ContractNegDomain;
import top.infopub.mgr.bpa.jsonVo.ContractReviewJsonVo;
import top.infopub.mgr.bpa.jsonVo.ListingJsonVo;
import top.infopub.mgr.bpa.service.BpaContractBpaReviewService;
import top.infopub.mgr.bpa.vo.ContractPublicVo;
import top.infopub.mgr.cap.jsonvo.LookInfoJsonVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.DiscloseProService;
import top.infopub.mgr.mkt.service.ProjectSubInfoService;
import top.infopub.model.bpa.BpaContractBpaReview;
import top.infopub.model.bpa.BpaContractListing;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 合同评审
 * @Author: Tszzzzzz
 * @Date: 2021/6/6
 */

@Controller
@RequestMapping("/contractBpaReview")
public class BpaContractBpaReviewController extends GenericFlowController {

    @Autowired
    private BpaContractBpaReviewService bpaContractBpaReviewService;
    @Autowired
    private DiscloseProService discloseProService;
    @Autowired
    private ProjectSubInfoService projectSubInfoService;
    /**
     * 合同评审列表数据
     * @return
     */
    @RequestMapping("/queryReviewByCondition")
    @ResponseBody
    public RspVo<ContractNegDomain> queryDataByCondition(ContractPublicVo contractPublicVo, ParamPage paramPage) throws ParseException {
        Page<ContractNegDomain> page = new Page<ContractNegDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = bpaContractBpaReviewService.selectByDomainWhere(page, contractPublicVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id) {
        request.setAttribute("id",id);
        return "bpa/lay-ContractReview-detail";
    }

    @RequestMapping("/queryDrop")
    @ResponseBody
    public RspVo queryDrop(){
        //查询已交底的项目
        List<BpaProjectDomain> bpaProjectDomain = discloseProService.selectProject();
        return RspVo.getSuccessResponse(bpaProjectDomain);
    }

    @RequestMapping("/getDrop")
    @ResponseBody
    public RspVo getDrop(){
        LookInfoJsonVo lookInfoJsonVo = new LookInfoJsonVo();
        return RspVo.getSuccessResponse(lookInfoJsonVo);
    }

    @RequestMapping("/queryReviewDetail")
    @ResponseBody
    public RspVo queryReviewDetail(String id) {
        //合同评审
        ContractReviewJsonVo contractNegJsonVo = bpaContractBpaReviewService.queryDetailById(id);
        return RspVo.getSuccessResponse(contractNegJsonVo);
    }

    @RequestMapping("save")
    @ResponseBody
    public RspVo save (String bodyStr, Integer submitType){
        AuthorizationVO userDtl = SessionUserUtils.getSessionAttributeForUserDtl();
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        ContractReviewJsonVo vo = JSON.parseObject(s,ContractReviewJsonVo.class);
        BpaContractBpaReview contractReview = new BpaContractBpaReview();
        if (StringUtils.isNotBlank(vo.getBpaContractBpaReview().getId())){//编辑
            BeanUtils.copyProperties(vo.getBpaContractBpaReview(),contractReview);
            contractReview.setUptBy(userDtl.getId());
            contractReview.setUptTime(new Date());
            contractReview.setProCode(vo.getProjectInfo().getProCode());
        }else{//新增
            BeanUtils.copyProperties(vo.getBpaContractBpaReview(),contractReview);
            contractReview.setId(IdFactory.getUUIDUpper());
            contractReview.setProCode(vo.getProjectInfo().getProCode());
            contractReview.setAddBy(userDtl.getId());
            contractReview.setAddTime(new Date());
        }
        /**
         * 附件处理
         * */
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(vo.getFileAtt()!=null){
            String[] fileAtt = vo.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(userDtl,contractReview.getProCode(),file);
                attachmentList.add(attachment);
            }
        }
        return bpaContractBpaReviewService.saveAll(userDtl,bodyStr,submitType,contractReview,vo.getBpaContractReviewPublic(),vo.getBpaContractSummary(),
                vo.getBpaContractListing(),vo.getBpaContractFormula(),vo.getBpaContractBid(),vo.getBpaContractBusiness(),attachmentList
        );
    }

    @RequestMapping("/getItemName")
    @ResponseBody
    public RspVo getItemName(String proCode){
        //查找该项目下的子项名称
      return projectSubInfoService.selectByProCode(proCode);
    }

    @RequestMapping("/saveListing")
    @ResponseBody
    public RspVo saveListing(String bodyStr,String id){
        String s = StringEscapeUtils.unescapeJava(bodyStr);
        List<BpaContractListing> listingJsonVos = JSON.parseArray(s, BpaContractListing.class);
        return bpaContractBpaReviewService.saveListing(listingJsonVos,id);
    }
}
